package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.NavUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseMonthTimeDialogFragment;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.entity.ProductSalesSearchContent;
import com.isunland.managesystem.utils.MyDateUtil;
import java.util.Date;

/**
 * Created by lenovo on 2016/4/21.
 */
public class ProductSaleSearchFragment extends Fragment implements View.OnClickListener {
  protected static final String EXTRA_SEARCH =
      "com.isunland.managesystem.ui.ProductSaleSearchFragment.EXTRA_SEARCH";
  private static final int START_DATA_BEGIN = 1;
  private static final int START_DATA_END = 2;
  private static final int SALES_MAN = 3;
  private static final int CUSTOMER = 4;
  private static final int PRODUCT = 5;

  private TextView tvStartDate;
  private ImageView ibStartDate;
  private TextView tvEndDate;
  private ImageView ibEndDate;
  private TextView tvSalesman;
  private ImageView ibSalesman;
  private TextView tvCutomer;
  private ImageView ibCutomer;
  private TextView tvSalesProduct;
  private ImageView ibSalesProduct;

  private String startDate = "";
  private String endDate = "";
  private String salePersonName = "";
  private String salePersonId = "";
  private String customerName = "";
  private String customerId = "";
  private String productName = "";
  private String productId = "";
  private ProductSalesSearchContent mSearchContent;

  @Override public void onCreate(Bundle savedInstanceState) {
    setHasOptionsMenu(true);
    if (NavUtils.getParentActivityName(getActivity()) != null) {
      ((BaseVolleyActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
    ((BaseVolleyActivity) getActivity()).getSupportActionBar().setTitle(R.string.query);
    mSearchContent = (ProductSalesSearchContent) getArguments().getSerializable(EXTRA_SEARCH);
    getData(mSearchContent);
    super.onCreate(savedInstanceState);
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_confirm, menu);
    super.onCreateOptionsMenu(menu, inflater);
  }

  private void getData(ProductSalesSearchContent mSearchContent) {
    startDate = mSearchContent.getStartDate();
    endDate = mSearchContent.getEndDate();
    salePersonName = mSearchContent.getSalePersonName();
    salePersonId = mSearchContent.getSalePersonId();
    customerName = mSearchContent.getCustomerName();
    customerId = mSearchContent.getCustomerId();
    productName = mSearchContent.getProductName();
    productId = mSearchContent.getProductId();
  }

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_search_project_sales, container, false);
    findView(v);
    initData();
    return v;
  }

  private void findView(View v) {
    tvStartDate = (TextView) v.findViewById(R.id.tv_start_date);
    ibStartDate = (ImageView) v.findViewById(R.id.ib_start_date);
    tvEndDate = (TextView) v.findViewById(R.id.tv_end_date);
    ibEndDate = (ImageView) v.findViewById(R.id.ib_end_date);
    tvSalesman = (TextView) v.findViewById(R.id.tv_salesman);
    ibSalesman = (ImageView) v.findViewById(R.id.ib_salesman);
    tvCutomer = (TextView) v.findViewById(R.id.tv_cutomer);
    ibCutomer = (ImageView) v.findViewById(R.id.ib_cutomer);
    tvSalesProduct = (TextView) v.findViewById(R.id.tv_salesProduct);
    ibSalesProduct = (ImageView) v.findViewById(R.id.ib_salesProduct);

    ibStartDate.setOnClickListener(ProductSaleSearchFragment.this);
    ibEndDate.setOnClickListener(ProductSaleSearchFragment.this);
    ibSalesman.setOnClickListener(ProductSaleSearchFragment.this);
    ibCutomer.setOnClickListener(ProductSaleSearchFragment.this);
    ibSalesProduct.setOnClickListener(ProductSaleSearchFragment.this);
  }

  @Override public void onClick(View v) {
    switch (v.getId()) {
      case R.id.ib_start_date:
        showDialog(START_DATA_BEGIN, startDate);
        break;

      case R.id.ib_end_date:
        showDialog(START_DATA_END, endDate);
        break;

      case R.id.ib_salesman:
        //人员 ，单选
        //                Intent i=new Intent(getActivity(),AppointerListActivity.class);
        //                startActivityForResult(i, SALES_MAN);
        //人员，多选
        Intent i = new Intent(getActivity(), SearchPersonMultipleActicity.class);
        i.putExtra(SearchPersonMultipleFragment.EXTRA_FROM,
            SearchPersonMultipleFragment.VALUE_COMMON);
        i.putExtra(SearchPersonMultipleFragment.EXTRA_JOB, salePersonId);
        startActivityForResult(i, SALES_MAN);
        break;
      case R.id.ib_cutomer:
        //跳转到客户名录列表界面,单选
        //                Intent customer=new Intent(getActivity(),CustomerListChooseActivity.class);
        //                startActivityForResult(customer, CUSTOMER);

        //客户列表  多选
        Intent customer = new Intent(getActivity(), CustomerMultipleListActivity.class);
        startActivityForResult(customer, CUSTOMER);
        break;

      case R.id.ib_salesProduct:
        //产品台账列表，多选
        Intent product = new Intent(getActivity(), ProductMultipleListActivity.class);
        startActivityForResult(product, PRODUCT);

        break;

      default:
        break;
    }
  }

  private void initData() {
    tvStartDate.setText(startDate);
    tvEndDate.setText(endDate);
    tvSalesman.setText(salePersonName);
    tvCutomer.setText(customerName);
    tvSalesProduct.setText(productName);
  }

  private void showDialog(int requestCode, String date) {
    //        DialogFragment dialog= new BaseMonthTimeDialogFragment();
    DialogFragment dialog = BaseMonthTimeDialogFragment.newInstance(MyDateUtil.stringToDate(date));
    FragmentManager fm = getActivity().getSupportFragmentManager();
    dialog.setTargetFragment(this, requestCode);
    dialog.show(fm, "");
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        //                if (NavUtils.getParentActivityName(getActivity())!=null) {
        //                    NavUtils.navigateUpFromSameTask(getActivity());
        //                }
        getActivity().finish();
        break;

      case R.id.menu_item_confirm:
        vollelPostConfirm();
        break;

      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  //回传数据
  private void vollelPostConfirm() {
    ProductSalesSearchContent content =
        new ProductSalesSearchContent(startDate, endDate, salePersonName, salePersonId,
            customerName, customerId, productName, productId);
    Intent i = new Intent();
    i.putExtra(EXTRA_SEARCH, content);
    getActivity().setResult(Activity.RESULT_OK, i);
    getActivity().finish();
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    //开始时间
    if (requestCode == START_DATA_BEGIN) {
      Date date = (Date) data.getSerializableExtra(BaseMonthTimeDialogFragment.EXTRA_DATE);
      startDate = MyDateUtil.dateToString(date, MyDateUtil.TYPE_MONTH);
      tvStartDate.setText(startDate);
    }
    //结束时间
    if (requestCode == START_DATA_END) {

      Date date = (Date) data.getSerializableExtra(BaseMonthTimeDialogFragment.EXTRA_DATE);
      endDate = MyDateUtil.dateToString(date, MyDateUtil.TYPE_MONTH);
      tvEndDate.setText(endDate);
    }
    //业务员
    if (requestCode == SALES_MAN) {
      //            CustomerDialog customer = (CustomerDialog) data.getSerializableExtra(BaseNetworkDialogFragment.EXTRA_VALUE);
      //            salePersonName=customer.getName();
      //            salePersonId=customer.getId();
      //            tvSalesman.setText(salePersonName);

      salePersonName = data.getStringExtra(SearchPersonMultipleFragment.EXTRA_NAME);
      salePersonId = data.getStringExtra(SearchPersonMultipleFragment.EXTRA_JOB);
      tvSalesman.setText(salePersonName);
    }
    //客户
    if (requestCode == CUSTOMER) {
      //            customerName=data.getStringExtra("customerName");
      //            customerId=data.getStringExtra("customerId");
      //            tvCutomer.setText(customerName);

      customerName = data.getStringExtra(CustomerMultipleListFragment.EXTRA_NAME);
      customerId = data.getStringExtra(CustomerMultipleListFragment.EXTRA_ID);
      tvCutomer.setText(customerName);
    }

    //产品
    if (requestCode == PRODUCT) {
      productName = data.getStringExtra(ProductListFragment.EXTRA_PRODUCT_NAMES);
      productId = data.getStringExtra(ProductListFragment.EXTRA_PRODUCT_CODES);
      tvSalesProduct.setText(productName);
    }
    super.onActivityResult(requestCode, resultCode, data);
  }

  public static ProductSaleSearchFragment newInstence(ProductSalesSearchContent item) {

    Bundle budle = new Bundle();
    budle.putSerializable(EXTRA_SEARCH, item);
    ProductSaleSearchFragment mFragment = new ProductSaleSearchFragment();
    mFragment.setArguments(budle);
    return mFragment;
  }
}
